function deepCopy(target) {
    // 只拷贝对象
    if (typeof target !== 'object' || target == null) return

    const deepCopyed = Array.isArray(target) ? [] : {}
    for (let key in target) {
        if (target.hasOwnProperty(key)) {
            if (typeof target[key] == 'objct' && target != null) {
                // 递归拷贝
                deepCopyed[key] = deepCopy(target[key])
            }
            deepCopyed[key] = target[key]
        }
    }
    return deepCopyed
}


function deepClone(obj, hash = new WeakMap()) {
    // 日期对象直接返回一个新的日期对象
    if (obj instanceof Date) {
        return new Date(obj);
    }
    //正则对象直接返回一个新的正则对象     
    if (obj instanceof RegExp) {
        return new RegExp(obj);
    }
    //如果循环引用,就用 weakMap 来解决
    if (hash.has(obj)) {
        return hash.get(obj);
    }
    // 获取对象所有自身属性的描述
    let allDesc = Object.getOwnPropertyDescriptors(obj);
    console.log('allDesc=', allDesc);
    // 遍历传入参数所有键的特性
    let cloneObj = Object.create(Object.getPrototypeOf(obj), allDesc)
    console.log('cloneObj=', cloneObj);
    hash.set(obj, cloneObj)
    for (let key of Reflect.ownKeys(obj)) {
        if (typeof obj[key] === 'object' && obj[key] !== null) {
            cloneObj[key] = deepClone(obj[key], hash);
        } else {
            cloneObj[key] = obj[key];
        }
    }
    console.log('res=', cloneObj);
    return cloneObj
}
const obj = {
    name: 'lucky',
    age: 19,
    hobby: {
        first: 'singing'
    },
    // hobby1: {
    //     first: 'singing'
    // }
}
deepClone(obj)


